package com.xyht.medicine_management_system.modules.medicineSource.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.medicine_management_system.common.commonReturn.CommonCodeEnum;
import com.xyht.medicine_management_system.common.commonReturn.CommonResult;
import com.xyht.medicine_management_system.common.commonReturn.ResponseResult;
import com.xyht.medicine_management_system.modules.classification.entity.MmsMedicineClassification;
import com.xyht.medicine_management_system.modules.classification.mapper.MmsMedicineClassificationMapper;
import com.xyht.medicine_management_system.modules.medicineSource.entity.MmsMedicineSourceClassification;
import com.xyht.medicine_management_system.modules.medicineSource.entity.req.MmsMedicineSourceClassificationExchangeReq;
import com.xyht.medicine_management_system.modules.medicineSource.entity.req.MmsMedicineSourceClassificationReq;
import com.xyht.medicine_management_system.modules.medicineSource.entity.resp.MmsMedicineSourceClassificationResp;
import com.xyht.medicine_management_system.modules.medicineSource.mapper.MmsMedicineSourceClassificationMapper;
import com.xyht.medicine_management_system.modules.medicineSource.service.MmsMedicineSourceClassificationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.medicine_management_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.medicine_management_system.modules.medicineSource.constant.IsTopConstant.IS_TOP_FALSE;
import static com.xyht.medicine_management_system.modules.medicineSource.constant.IsTopConstant.IS_TOP_TRUE;

/**
 * <p>
 * 药知源分类 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-01-04
 */
@Service
public class MmsMedicineSourceClassificationServiceImpl extends ServiceImpl<MmsMedicineSourceClassificationMapper, MmsMedicineSourceClassification> implements MmsMedicineSourceClassificationService {
    @Resource
    private MmsMedicineSourceClassificationMapper mmsMedicineSourceClassificationMapper;


    @Override
    public ResponseResult addClassification(MmsMedicineSourceClassificationReq classificationReq) {
        if (isNullOrEmpty(classificationReq.getClassificationName(),classificationReq.getClassificationTop())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        if (!classificationReq.getClassificationTop().equals(IS_TOP_FALSE)
                && !classificationReq.getClassificationTop().equals(IS_TOP_TRUE)){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        List<MmsMedicineSourceClassification> classificationList = mmsMedicineSourceClassificationMapper.selectList(new LambdaQueryWrapper<MmsMedicineSourceClassification>()
                .orderByDesc(MmsMedicineSourceClassification::getClassificationSort));
        int sort = 1;
        if (classificationList != null && classificationList.size() > 0){
            sort = classificationList.get(0).getClassificationSort() + 1;
        }

        MmsMedicineSourceClassification addClassification = new MmsMedicineSourceClassification();
        addClassification.setClassificationSort(sort);
        addClassification.setClassificationTop(classificationReq.getClassificationTop());
        addClassification.setClassificationName(classificationReq.getClassificationName());
        mmsMedicineSourceClassificationMapper.insert(addClassification);
        return CommonResult.success();
    }

    @Override
    public ResponseResult deleteClassification(MmsMedicineSourceClassificationReq classificationReq) {
        if (isNullOrEmpty(classificationReq.getId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        mmsMedicineSourceClassificationMapper.deleteById(classificationReq.getId());
        return CommonResult.success();
    }

    @Override
    public ResponseResult updateClassification(MmsMedicineSourceClassificationReq classificationReq) {
        if (isNullOrEmpty(classificationReq.getId(),classificationReq.getClassificationName(),
                classificationReq.getClassificationTop())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        if (!classificationReq.getClassificationTop().equals(IS_TOP_FALSE)
                && !classificationReq.getClassificationTop().equals(IS_TOP_TRUE)){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        MmsMedicineSourceClassification mmsMedicineSourceClassification = mmsMedicineSourceClassificationMapper.selectById(classificationReq.getId());
        if (mmsMedicineSourceClassification == null){
            return CommonResult.failed(CommonCodeEnum.MEDICINE_SOURCE_CLASSIFICATION_NOT_EXIST);
        }

        MmsMedicineSourceClassification updateClassification = new MmsMedicineSourceClassification();
        updateClassification.setId(classificationReq.getId());
        updateClassification.setClassificationTop(classificationReq.getClassificationTop());
        updateClassification.setClassificationName(classificationReq.getClassificationName());
        mmsMedicineSourceClassificationMapper.updateById(updateClassification);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getSourceClassificationList(Integer pageNum, Integer pageSize, String searchStr) {
        QueryWrapper<MmsMedicineSourceClassification> queryWrapper = new QueryWrapper<>();
        if (!isNullOrEmpty(searchStr)){
            queryWrapper.lambda().like(MmsMedicineSourceClassification::getClassificationName,searchStr);
        }
        queryWrapper.lambda()
                .orderByDesc(MmsMedicineSourceClassification::getClassificationTop)
                .orderByAsc(MmsMedicineSourceClassification::getClassificationSort);

        Page<MmsMedicineSourceClassification> page = new Page<>(pageNum,pageSize);
        mmsMedicineSourceClassificationMapper.selectPage(page,queryWrapper);

        List<MmsMedicineSourceClassificationResp> list = page.getRecords()
                .stream()
                .map(classification->{
                    MmsMedicineSourceClassificationResp classificationResp = new MmsMedicineSourceClassificationResp();
                    BeanUtils.copyProperties(classification,classificationResp);
                    return classificationResp;
                })
                .collect(Collectors.toList());
        int total = (int) page.getTotal();
        return CommonResult.success(list,total);
    }

    @Override
    public ResponseResult getSourceClassificationDetail(String id) {
        if (isNullOrEmpty(id)){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        MmsMedicineSourceClassification mmsMedicineSourceClassification = mmsMedicineSourceClassificationMapper.selectById(id);
        if (mmsMedicineSourceClassification == null){
            return CommonResult.failed(CommonCodeEnum.MEDICINE_SOURCE_CLASSIFICATION_NOT_EXIST);
        }

        MmsMedicineSourceClassificationResp classificationResp = new MmsMedicineSourceClassificationResp();
        BeanUtils.copyProperties(mmsMedicineSourceClassification,classificationResp);
        return CommonResult.success(classificationResp);
    }

    @Override
    @Transactional
    public ResponseResult exchangeClassificationSort(MmsMedicineSourceClassificationExchangeReq exchangeReq) {
        if (isNullOrEmpty(exchangeReq.getFirstClassificationId(),exchangeReq.getSecondClassificationId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        MmsMedicineSourceClassification firstClassification = mmsMedicineSourceClassificationMapper.selectById(exchangeReq.getFirstClassificationId());
        if (firstClassification == null){
            return CommonResult.failed(CommonCodeEnum.MEDICINE_SOURCE_CLASSIFICATION_NOT_EXIST);
        }
        MmsMedicineSourceClassification secondSourceClassification = mmsMedicineSourceClassificationMapper.selectById(exchangeReq.getSecondClassificationId());
        if (secondSourceClassification == null){
            return CommonResult.failed(CommonCodeEnum.MEDICINE_SOURCE_CLASSIFICATION_NOT_EXIST);
        }

        Integer firstSort = firstClassification.getClassificationSort();
        Integer secondSort = secondSourceClassification.getClassificationSort();

        firstClassification.setClassificationSort(secondSort);
        mmsMedicineSourceClassificationMapper.updateById(firstClassification);

        secondSourceClassification.setClassificationSort(firstSort);
        mmsMedicineSourceClassificationMapper.updateById(secondSourceClassification);
        return CommonResult.success();
    }
}
